# 定义libsoc_adapter依赖
libsoc_adapter_dep = dependency('libsoc_adapter', method : 'pkg-config', required : false)

subdir('bus')
subdir('chip')

# 合并所有源文件到一个列表
internal_sources = [
  # manager 源文件
  'manager.cpp',
  # bus 模块源文件
  bus_sources,
  # chip 模块源文件
  chip_sources
]

# 合并所有头文件到一个列表
internal_headers = [
  # manager 头文件
  'manager.h',
  'hasdef.h',
  # bus 模块头文件
  bus_headers,
  # chip 模块头文件
  chip_headers
]

# 如果是meson构建，添加测试打桩文件
if meson_build
  internal_sources += [
    '../../tests/drivers/bus/i2c/drivers/i2c.cpp',
    '../../tests/drivers/bus/hisport/drivers/hisport.cpp',
  ]
endif

# 安装头文件，保持原来的目录结构
install_headers('bus/bus_base/bus.h', install_dir: install_inc_dir / 'internal/bus/bus_base')
install_headers('bus/bus_i2c/i2c.h', install_dir: install_inc_dir / 'internal/bus/bus_i2c')
install_headers('bus/bus_i2c_mux/i2c_mux.h', install_dir: install_inc_dir / 'internal/bus/bus_i2c_mux')
install_headers('bus/bus_hisport/hisport.h', install_dir: install_inc_dir / 'internal/bus/bus_hisport')
install_headers('chip/chip_base/chip.h', install_dir: install_inc_dir / 'internal/chip/chip_base')
install_headers('chip/chip_lm75/lm75.h', install_dir: install_inc_dir / 'internal/chip/chip_lm75')
install_headers('chip/chip_pca9545/pca9545.h', install_dir: install_inc_dir / 'internal/chip/chip_pca9545')
install_headers('chip/chip_pca9555/pca9555.h', install_dir: install_inc_dir / 'internal/chip/chip_pca9555')
install_headers('chip/chip_ina/ina.h', install_dir: install_inc_dir / 'internal/chip/chip_ina')
install_headers('chip/chip_eeprom/eeprom.h', install_dir: install_inc_dir / 'internal/chip/chip_eeprom')
install_headers('chip/chip_ads78/ads78.h', install_dir: install_inc_dir / 'internal/chip/chip_ads78')
install_headers('manager.h', install_dir: install_inc_dir / 'internal')
install_headers('hasdef.h', install_dir: install_inc_dir / 'internal')

# 根据构建类型设置include路径
internal_include_dirs = [
    include_directories('.'),
    include_directories('..'),
]

# 如果是meson构建（包括compile和test），添加测试打桩文件的include路径
if meson_build
  internal_include_dirs += include_directories('../../tests/drivers/bus/hisport')
  internal_include_dirs += include_directories('../../tests/drivers/bus/i2c')
endif

# 如果是非meson构建且有libsoc_adapter依赖，添加其include路径
if not meson_build and libsoc_adapter_dep.found()
  internal_include_dirs += libsoc_adapter_dep.get_variable('includedir', default_value: '')
endif

internal_static_lib = static_library(
  'Internal',
  internal_sources,
  include_directories: internal_include_dirs,
  dependencies: [libsoc_adapter_dep, libmcpp_deps, device_tree_gen_dep],
  link_args: [],
  install: false
)

# 构建统一的internal库
internal_lib = shared_library(
  'Internal',
  include_directories: internal_include_dirs,
  link_args: [],
  name_prefix: 'lib',
  name_suffix: 'so',
  install: true,
  link_whole: [
    internal_static_lib
  ]
)

internal_dep = declare_dependency(
  link_with: internal_lib,
  include_directories: internal_include_dirs,
  dependencies: [libsoc_adapter_dep, libmcpp_deps, device_tree_gen_dep],
)

if build_tests and meson_build
  # 复制internal库到测试目录
  copy_internal = custom_target(
    'copy_libInternal',
    output: 'copy_libInternal',
    command: ['cp', internal_lib.full_path(), drivers_install_dir],
    depends: internal_lib,
    build_by_default: true,
  )

  alias_target('copy_internal_lib', copy_internal)
endif
